package com.vitalfit.controller;

import com.vitalfit.annotation.LoginUser;
import com.vitalfit.enums.ResultEnum;
import com.vitalfit.model.entity.fitness.FitnessPlan;
import com.vitalfit.model.vo.ResultVO;
import com.vitalfit.model.vo.UserAuthentication;
import com.vitalfit.repository.FitnessPlanRepository;
import com.vitalfit.utils.ResultVOUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/fitness_plan")
@RequiredArgsConstructor
public class FitnessPlanController {

    private final FitnessPlanRepository fitnessPlanRepository;
    @PreAuthorize("hasRole('ROLE_SHOPMANAGER')")
    @GetMapping("/mail/expire")
    public List<String> getCustomerEmailsByExpiringPlans() {
        LocalDate today = LocalDate.now();
        LocalDate inSevenDays = today.plusDays(7);
        return fitnessPlanRepository.findCustomerEmailsByExpiringPlans(today, inSevenDays);
    }

    @PreAuthorize("hasRole('ROLE_SHOPMANAGER')")
    @PostMapping("all")
    public ResultVO<List<FitnessPlan>> getAllFitnessPlan() {
        return ResultVOUtil.buildResult(ResultEnum.POST_SUCCESS,fitnessPlanRepository.findAll());
    }

    @PreAuthorize("hasRole('ROLE_SHOPMANAGER') or hasRole('ROLE_CUSTOMER')")
    @PostMapping("/user")
    public ResultVO<List<FitnessPlan>> getFitnessPlanByUser(@LoginUser UserAuthentication userAuthentication) {
        return ResultVOUtil.buildResult(ResultEnum.POST_SUCCESS,fitnessPlanRepository.findFitnessPlanByCustomerId(userAuthentication.getUserId()));
    }




}
